<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019-08-08
 * Time: 10:37
 */

namespace App\Services;

use App\Repositories\PermissionRepository;
use App\Repositories\RoleRepository;
use Illuminate\Support\Facades\DB;
use Mockery\Exception;

/**
 * 遵循SOLID原则（分割、单元测试、mock测试）
 * 涉及到role角色的外部行为逻辑:例如验证、授权权限、将多个权限授权给角色、统计等外部服务和逻辑处理
 * 该逻辑将注入到 @class PermissionController
 * Class RoleService
 * @package App\Services
 */
class RoleService extends BaseAdminService
{
    /**
     * @var PermissionRepository 注入的PermissionRepository
     */
    protected $permissionRepository;

    /**
     * @var RoleRepository 注入的RoleRepository
     */
    protected $roleRepository;

    /**
     * RoleService constructor.
     * @param PermissionRepository $permissionRepository
     * @param RoleRepository $roleRepository
     */
    public function __construct(PermissionRepository $permissionRepository, RoleRepository $roleRepository){
        $this->permissionRepository = $permissionRepository;
        $this->roleRepository = $roleRepository;
    }

    /**
     * 获取角色列表
     * @param $name
     * @param $limit
     * @return mixed
     */
    public function getRolePages($name, $limit){
        return $this->roleRepository->getRolePages($name, $limit);
    }

    /**
     * 创建角色，并授权权限
     * @param array $data
     * @return array
     */
    public function postRole(array $data){
        $permissionIds = data_get($data, 'permission_ids');

        //验证权限
        $permissions = $this->validatePermissions($permissionIds);

        //处理并授权权限
        DB::beginTransaction();
        try {

            //保存角色
            $role = $this->roleRepository->postRole($data);

            //将多个权限授权给角色
            $permissions && $role->syncPermissions($permissions);

            DB::commit();

            return $this->returnServerData(true);

        }catch (Exception $exception){

            DB::rollBack();

            return $this->returnServerData(false, $exception->getMessage());
        }
    }

    /**
     * 编辑角色及其权限
     * @param array $data
     * @return array
     */
    public function updateRole(array $data){
        $permissionIds = data_get($data, 'permission_ids');

        //验证角色
        if($this->roleRepository->getRoleIdByName($data['name']) != $data['id']){
            return $this->returnServerData(false, '您修改的角色名称其他角色已占用！');
        }

        //验证权限
        $permissions = $this->validatePermissions($permissionIds);

        //处理并授权权限
        DB::beginTransaction();
        try {

            //保存角色
            $role = $this->roleRepository->updateRole($data);

            //将多个权限授权给角色
            $permissions && $role->syncPermissions($permissions);

            DB::commit();

            return $this->returnServerData(true);

        }catch (Exception $exception){

            DB::rollBack();

            return $this->returnServerData(false, $exception->getMessage());
        }

    }

    /**
     * 验证权限
     * @param $permissionIds
     * @return array|mixed
     */
    protected function validatePermissions(array $permissionIds){
        $permissions = [];

        //如果存在权限，验证权限
        if($permissionIds){
            $permissions = $this->permissionRepository->getAllPermissionByIds($permissionIds);

            if ($permissions->IsEmpty()) {
                return $this->returnServerData(false, '参数错误，无法找到权限！');
            }
        }

        return $permissions;

    }

    /**
     * 获取单条角色数据
     * @param int $id
     * @return mixed
     */
    public function getSingleRole(int $id){
        return $this->roleRepository->getSingleRole($id);
    }

    /**
     * 获取所有列表
     * @return mixed
     */
    public function getRoleList(){
        return $this->roleRepository->getRoleList();
    }

    /**
     * 删除角色
     * @param array $ids
     * @return int
     */
    public function deleteRole(array $ids){
        return $this->roleRepository->deleteRole($ids);
    }

}